package com.tanhua.dubbo.api;

import com.tanhua.model.mongo.Friend;
import com.tanhua.model.mongo.UserLike;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;

import java.util.List;

@DubboService
public class UserLikeApiImpl implements UserLikeApi {
    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public boolean save(Long likeUserId,Long userId) {
        try {
            //1、查询数据是否存在
            Query query = Query.query(Criteria.where("userId").is(userId).and("likeUserId").is(likeUserId));
            UserLike userLike = mongoTemplate.findOne(query, UserLike.class);
            if (userLike != null) {//如果数据存在
                //修改数据
                //3、更新
                Update update = Update.update("isLike", true)
                        .set("updated", System.currentTimeMillis());
                mongoTemplate.updateFirst(query, update, UserLike.class);
            } else {
                //添加用户
                UserLike userLike1 = new UserLike();
                userLike1.setUserId(userId);
                userLike1.setIsLike(true);
                userLike1.setLikeUserId(likeUserId);
                userLike1.setCreated(System.currentTimeMillis());
                userLike1.setUpdated(System.currentTimeMillis());
                mongoTemplate.save(userLike1);
            }
            return true;
        } catch (Exception e) {
            return false;
        }

    }

    @Override
    public boolean saveToUnLove(Long likeUserId ,Long userId) {

        try {
            //1、查询数据是否存在
            Query query = Query.query(Criteria.where("userId").is(userId).and("likeUserId").is(likeUserId));
            UserLike userLike = mongoTemplate.findOne(query, UserLike.class);
            if (userLike != null) {//如果数据存在
                //修改数据
                //3、更新
                Update update = Update.update("isLike", false)
                        .set("updated", System.currentTimeMillis());
                mongoTemplate.updateFirst(query, update, UserLike.class);
            } else {
                //添加用户
                UserLike userLike1 = new UserLike();
                userLike1.setUserId(userId);
                userLike1.setIsLike(false);
                userLike1.setLikeUserId(likeUserId);
                userLike1.setCreated(System.currentTimeMillis());
                userLike1.setUpdated(System.currentTimeMillis());
                mongoTemplate.save(userLike1);
            }
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public List<UserLike> findUserLike(Integer page, Integer pagesize, Long userId) {
        Query query = Query.query(Criteria.where("likeUserId").is(userId).and("isLike").is(true))
                .skip((page-1)*pagesize)
                .limit(pagesize)
                .with(Sort.by(Sort.Order.desc("created")));
        List<UserLike> userLikeList = mongoTemplate.find(query, UserLike.class);
        return userLikeList;
    }

    //喜欢我
    public Integer findLikeme(Long userId) {
        Query query=Query.query(Criteria.where("likeUserId").is(userId).and("isLike").is(true));
        long count = mongoTemplate.count(query, UserLike.class);
        int Likeme=(int) count;
        return Likeme;
    }

    //我喜欢
    public Integer MeLike(Long userId) {
        Query query=Query.query(Criteria.where("userId").is(userId).and("isLike").is(true));
        long count = mongoTemplate.count(query, UserLike.class);
        int MeLike=(int) count;
        return MeLike;
    }

    @Override
    public Integer eachLove(Long userId) {
        Query query=Query.query(Criteria.where("userId").is(userId));
        long count = mongoTemplate.count(query, Friend.class);
        int eachLove=(int) count;
        return eachLove;
    }
}
